package com.zheng.hr.rpc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;

import com.zheng.hr.dao.model.HrPersonalContract;
import com.zheng.hr.dao.mapper.HrPersonalContractMapper;
import com.zheng.hr.dao.model.HrPersonalContractRecords;
import com.zheng.hr.rpc.api.HrPersonalContractRecordsService;
import com.zheng.hr.rpc.api.HrPersonalContractService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.oa.common.exception.OaException;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * hr_personal_contract 人事合同 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-01-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class HrPersonalContractServiceImpl extends BaseServiceImpl<HrPersonalContractMapper, HrPersonalContract> implements HrPersonalContractService {
    @Autowired
    private HrPersonalContractRecordsService contractRecordsService;
    @Autowired
    private UpmsApiService apiService;

    @Override
    public boolean insertPersonalContract(HrPersonalContract personalContract, String username) throws OaException {
        Integer personalId = personalContract.getPersonalId();
        EntityWrapper<HrPersonalContractRecords> contractRecordsEntityWrapper = new EntityWrapper<>();
        contractRecordsEntityWrapper.where("contract_number='" + personalContract.getContractNumber() + "'");
        if (null != contractRecordsService.selectOne(contractRecordsEntityWrapper)) {
            throw new OaException("合同编号重复！请更换编号");
        }
        EntityWrapper<HrPersonalContract> contractEntityWrapper = new EntityWrapper<>();
        contractEntityWrapper.where("personal_id=" + personalId);
        //删除当前的其他合同
        delete(contractEntityWrapper);
        insert(personalContract);
        HrPersonalContractRecords personalContractRecords = change(personalContract);
        UpmsUser upmsUser = apiService.selectUpmsUserByUsername(username);
        personalContractRecords.setCuser(upmsUser.getUserId());
        personalContractRecords.setCtime(new Date());
        return contractRecordsService.insert(personalContractRecords);
    }

    @Override
    public boolean updatePersonalContract(HrPersonalContract personalContract) throws OaException {
        EntityWrapper<HrPersonalContractRecords> contractRecordsEntityWrapper = new EntityWrapper<>();
        contractRecordsEntityWrapper.where("contract_number='" + personalContract.getContractNumber() + "'").and("id!=" + personalContract.getId());
        if (null != contractRecordsService.selectOne(contractRecordsEntityWrapper)) {
            throw new OaException("合同编号重复！请更换编号");
        }
        updateById(personalContract);
        EntityWrapper<HrPersonalContractRecords> personalContractRecordsEntityWrapper = new EntityWrapper<>();
        personalContractRecordsEntityWrapper.where("contract_id=" + personalContract.getId());
        HrPersonalContractRecords contractRecords = change(personalContract);
        Integer id = contractRecordsService.selectOne(personalContractRecordsEntityWrapper).getId();
        contractRecords.setId(id);
        contractRecords.setCtime(null);
        return contractRecordsService.updateById(contractRecords);
    }

    private HrPersonalContractRecords change(HrPersonalContract personalContract) {
        HrPersonalContractRecords personalContractRecords = new HrPersonalContractRecords();
        personalContractRecords.setContractNumber(personalContract.getContractNumber());
        personalContractRecords.setContractType(personalContract.getContractType());
        personalContractRecords.setPersonalId(personalContract.getPersonalId());
        personalContractRecords.setContractId(personalContract.getId());
        personalContractRecords.setCtime(new Date());
        personalContractRecords.setEffectiveTime(personalContract.getEffectiveTime());
        personalContractRecords.setPeriod(personalContract.getPeriod());
        personalContractRecords.setUneffectiveTime(personalContract.getUneffectiveTime());
        personalContractRecords.setRemarks(personalContract.getRemarks());
        return personalContractRecords;
    }
}
